﻿@page "/archive"
@using System.Globalization
@using Moonglade.Core.PostFeature
@inject IMediator Mediator
@{
    ViewBag.TitlePrefix = "Archive ";
    ViewBag.BodyClass = "body-archive-index bg-gray-1";

    var archives = await Mediator.Send(new GetArchiveQuery());
    int[] yearList = null;
    if (null != Model && archives.Any())
    {
        yearList = (from item in archives
                    orderby item.Year descending
                    select item.Year).Distinct().ToArray();
    }

    static string GetMonthNameByNumber(int number)
    {
        return number is > 12 or < 1 ?
            string.Empty :
            CultureInfo.CurrentUICulture.DateTimeFormat.GetMonthName(number);
    }
}

@section head {
        <link href="~/css/timeline.css" rel="stylesheet" asp-append-version="true" />
}

<h2 class="page-heading mb-4">
    @SharedLocalizer["Archive"]
</h2>

@if (null != Model && archives.Any() && null != yearList)
{
    <ul class="timeline" id="monthList">
    @for (var index = 0; index < yearList.Length; index++)
    {
        var item = yearList[index];
        var queryMonth = archives.Where(obj => obj.Year == item);

        <li class="@(index % 2 == 0 ? null: "timeline-inverted")">
            <div class="timeline-badge"></div>
            <div class="timeline-panel shadow-sm bg-white">
                <div class="timeline-heading">
                    <h3 class="timeline-title">
                        <a asp-page="./ArchiveList" asp-route-year="@item">@item</a>
                    </h3>
                </div>
                <div class="timeline-body">
                    <div class="row g-2">
                        @foreach (var month in queryMonth)
                        {
                            <div class="col-6">
                                <span class="badge bg-secondary float-end">@month.Count</span>
                                <a asp-page="./ArchiveList"
                                   asp-route-year="@month.Year"
                                   asp-route-month="@month.Month">
                                    @GetMonthNameByNumber(month.Month)</a>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </li>
    }
    </ul>
}
else
{
    <div class="text-muted text-center">
        - @SharedLocalizer["No Archive"] -
    </div>
}
